// JavaScript Document

	var a_map;
	var geocoder;
	var a_directionsService = new google.maps.DirectionsService();
	var from;
	var to;
	var i1 = -1;
  
	function auto_calcRoute(nextorprev,mapornomap) 
	{
		directionsDisplay = new google.maps.DirectionsRenderer();
		var opts = 
		{
		 //center: new google.maps.LatLng(28.631257700000000000, 77.223091299999960000),
		 zoom: 12,
		 mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		a_map = new google.maps.Map(document.getElementById('auto_map'), opts);
		google.maps.event.trigger(a_map,'resize');
			
		//directionsDisplay.setPanel(document.getElementById('directions-panel'));
		 
		var trafficLayer = new google.maps.TrafficLayer();
        trafficLayer.setMap(a_map);

		geocoder = new google.maps.Geocoder();
	  		
		var origin1 = document.getElementById("from").value;
		var destinationA = document.getElementById("to").value;
		var distanceInput_main = document.getElementById("automain_outputdist");
		var distanceInput_map = document.getElementById("automap_outputdist");
		$('#automain_outputdist').empty();
		$('#automap_outputdist').empty();
		var durationInput_main = document.getElementById('automain_outputtime');
		var durationInput_map = document.getElementById('automap_outputtime');
		$('#automain_outputtime').empty();
		$('#automap_outputtime').empty();
			
		directionsDisplay.setMap(a_map);
        //var selectedMode = document.getElementById('mode').value;
        var request =
		{
			origin: origin1,
			destination: destinationA,
			travelMode: google.maps.TravelMode.DRIVING,
			provideRouteAlternatives: true,
			durationInTraffic: true
        };
		
		a_directionsService.route(request, function(response, status) 
		{
	      	if (status == google.maps.DirectionsStatus.OK) 
			{	  
				if(nextorprev==1) //Go to the next path
					i1++;
				else if(nextorprev==-1)// Go to the previous path
					i1--;
				/*if(response.routes.length < i+1)
								{
									console.log("No such route");
									i=-1;
								}
				 // Display the distance:
				 else{*/
		 
				if(response.routes.length < i1+1) //if no. of paths exceeded : RESET
					i1=0;
				if(i1==-1) //On pressing the previous button, go to the last route of the array
				{
					i1 = response.routes.length - 1;
				}
				if(i1<=-2) //test case
				{
					i1=-1;
				}
         		document.getElementById('automain_outputdist').innerHTML += 
            	response.routes[i1].legs[0].distance.text;
         		document.getElementById('automap_outputdist').innerHTML += 
            	response.routes[i1].legs[0].distance.text;
				
        		// Display the duration:
         		document.getElementById('automain_outputtime').innerHTML += 
            	response.routes[i1].legs[0].duration.text;
         		document.getElementById('automap_outputtime').innerHTML += 
            	response.routes[i1].legs[0].duration.text;
				
				var strdistance = document.getElementById('automain_outputdist').innerHTML;
				var distance = parseFloat(strdistance);

				//console.log("auto distance is: " + distance);
				
				//---------------------------------FARE CALCULATION----------------------------------------
				
				var fare;
				var hrs = localStorage.getItem('hrs');
				var mins = localStorage.getItem('mins');
				console.log("Hours entered is: " + hrs);
				console.log("Mins entered is: " + mins);
				
				if (distance <= 2)
					fare = 25;
				else
				{
					var distanceinto10 = (distance-2.0) * 10;
					fare = 25 + distanceinto10*0.8;
				}
				roundedfare = Math.round(fare * 100) / 100;
				
				if (hrs<5 || hrs==23)//if time is 11pm till 4.59am
				{
					roundedfare = roundedfare + (0.25*roundedfare);					
					document.getElementById('automain_fare').innerHTML = Math.round(roundedfare);
					document.getElementById('automap_fare').innerHTML = "Rs. " + Math.round(roundedfare) + " (inclusive of Night charge)";	
				}
				else //if time is 5am till 10.59pm
				{	
					document.getElementById('automain_fare').innerHTML = Math.round(roundedfare); 
					document.getElementById('automap_fare').innerHTML = "Rs. " + Math.round(roundedfare);
				}
				
				//------------------------------------------------------------------------------------------		
				
			
				//For duration in traffic need to buy Business maps API
				
				//distanceInput.value = response.routes[i].legs[0].distance.text;
				//durationInput.value = response.routes[i].legs[0].duration.text;
				
				//localStorage.setItem('dist', distanceInput.value);
				//localStorage.setItem('time', durationInput.value);

				//directionsDisplay.setRouteIndex(1);
                //console.log(directionsDisplay.getRouteIndex());
                //var temp = directionsDisplay.getDirections();
				
				if(i1==0) //1st path - no switching
				{
					if(mapornomap==1)
					{
						directionsDisplay.setDirections(response);
						google.maps.event.trigger(a_map,'resize');
					}
							//directionsDisplay.setDirections(response);
				}
				if(i1>=1) // 2nd route : Switching
				{
					if(mapornomap==1)
					{
						var temp = response.routes[0];
						response.routes[0]= response.routes[i1];
						response.routes[i1]=temp;
						directionsDisplay.setDirections(response);
						google.maps.event.trigger(a_map,'resize');
					}
					else if(mapornomap==0) // test case
					{
						var temp = response.routes[0];
						response.routes[0]= response.routes[i1];
						response.routes[i1]=temp;
						google.maps.event.trigger(a_map,'resize');
					}
					//directionsDisplay.setDirections(response);
				}
                //console.log(directionsDisplay.getRouteIndex());
			}
      //directionsDisplay.setDirections(response);
	  });
	}	  
	//google.maps.event.addDomListener(window, 'load', initialize);
	
	function auto_getdirections()
	{
		directionsDisplay.setPanel(document.getElementById('auto_directions-panel'));
	}